package com.cneport.das.server.util;

import com.cneport.das.common.utils.JsonUtil;
import com.cneport.das.server.util.amq.ActiveMqHelper;
import com.cneport.das.server.util.rmq.RabbitMqHelper;
import com.cneport.das.server.util.wmq.WebSphereMQHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GavinZhang
 * @version 1.0
 * @desc： MQ Service
 * @company 东方物通科技有限（北京）公司
 * @date 2021/9/7 15:47
 */
@Component
@Slf4j
public class MQService {
    @Value("${mq.rmq.enable}")
    private Boolean rabbitMQ;
    @Value("${mq.wmq.enable}")
    private Boolean wheSphereMQ;
    @Value("${mq.amq.enable}")
    private Boolean activeMQ;

    @Autowired(required = false)
    private ActiveMqHelper activeMqHelper;
    @Autowired(required = false)
    private RabbitMqHelper rabbitMqHelper;
    @Autowired(required = false)
    private WebSphereMQHelper wMQHelper;

    /**
     * 往队列里面发送消息
     *
     * @param destination
     * @param msg
     * @param selectedMQType
     * @return
     */
    public boolean send2Queue(String destination, String msg, String selectedMQType) {
        //使用activeMQ
        if (MqTypeEnum.ACTIVE_MQ.getValue().equals(selectedMQType)) {
            if (!activeMQ) {
                log.error("请检查 activeMQ 的配置项[ats.mq.amq.enable]是否为true");
                return false;
            }
            try {
                activeMqHelper.sendToQueue(destination, msg);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("往activeMQ的队列[" + destination + "]发送消息失败!消息-->[" + msg + "]");
                return false;
            }
            return true;
            //使用rabbitMQ
        } else if (MqTypeEnum.RABBIT_MQ.getValue().equals(selectedMQType)) {
            if (!rabbitMQ) {
                log.error("请检查 rabbitMQ 的配置项[ats.mq.rmq.enable]是否为true");
                return false;
            }
            try {
                rabbitMqHelper.sendMsg(destination, msg);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("往rabbitMQ的队列[" + destination + "]发送消息失败!消息-->[" + msg + "]");
                return false;
            }
            return true;

        } else if (MqTypeEnum.WEBSPHERE_MQ.getValue().equals(selectedMQType)) {
            if (!wheSphereMQ) {
                log.error("请检查 websphereMQ 的配置项[ats.mq.wmq.enable]是否为true");
                return false;
            }
            return wMQHelper.send(destination, msg);
        } else {
            return false;
        }
    }

    /**
     * activeMQ 往主题里面发送消息
     *
     * @param destination
     * @param msg
     * @return
     */
    public boolean amqSend2Topic(String destination, String msg) {
        if (!activeMQ) {
            log.error("请检查 activeMQ 的配置项[ats.mq.amq.enable]是否为true");
            return false;
        }
        try {
            activeMqHelper.sendTopic(destination, msg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("往activeMQ的队列[" + destination + "]发送消息失败!消息-->[" + msg + "]");
            return false;
        }
        return true;
    }

    /**
     * rabbitMQ 发送消息
     *
     * @param exchange
     * @param routingKey
     * @param msg
     * @return
     */
    public boolean rmqSend(String exchange, String routingKey, String msg) {
        if (!rabbitMQ) {
            log.error("请检查 rabbitMQ 的配置项[ats.mq.rmq.enable]是否为true");
            return false;
        }
        try {
            rabbitMqHelper.sendMsg(exchange, routingKey, msg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("往rabbitMQ发送消息失败!exchange:[" + exchange + "] routingKey:[" + routingKey + "] 消息-->[" + msg + "]");
            return false;
        }
        return true;
    }

    /**
     * 从队列里面收消息
     *
     * @param destination
     * @param cnt            一次收取条数，不够直接取出所有的。
     * @param selectedMQType
     * @return
     */
    public List<String> receiveFromQueue(String destination, int cnt, String selectedMQType) {
        List<String> messages = new ArrayList<>(0);
        if (MqTypeEnum.ACTIVE_MQ.getValue().equals(selectedMQType)) {
            if (!activeMQ) {
                log.error("请检查 activeMQ 的配置项[ats.mq.amq.enable]是否为true");
                return messages;
            }
            String message = activeMqHelper.receiveMsg(destination, 0, cnt);
            messages = JsonUtil.toList(message, String.class);
            return messages;

        } else if (MqTypeEnum.RABBIT_MQ.getValue().equals(selectedMQType)) {
            if (!rabbitMQ) {
                log.error("请检查 rabbitMQ 的配置项[ats.mq.rmq.enable]是否为true");
                return messages;
            }
            String message = rabbitMqHelper.receive(destination, cnt);
            messages = JsonUtil.toList(message, String.class);
            return messages;

        } else {
            if (!wheSphereMQ) {
                log.error("请检查 websphereMQ 的配置项[ats.mq.wmq.enable]是否为true");
                return messages;
            }
            String message = wMQHelper.receive(destination, cnt);
            messages = JsonUtil.toList(message, String.class);
            return messages;
        }

    }

    /**
     * activeMQ 从主题中取消息
     *
     * @param destination
     * @param cnt
     * @return
     */
    public List<String> amqReceiveFromTopic(String destination, int cnt) {
        List<String> messages = new ArrayList<>(0);
        if (!activeMQ) {
            log.error("请检查 activeMQ 的配置项[ats.mq.amq.enable]是否为true");
            return messages;
        }
        String message = activeMqHelper.receiveMsg(destination, 1, cnt);
        messages = JsonUtil.toList(message, String.class);
        return messages;
    }

}
